#include "threaddlg.h"
#include "ui_threaddlg.h"
#include <QHBoxLayout>
ThreadDlg::ThreadDlg(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ThreadDlg)
{
    ui->setupUi(this);
    setWindowTitle(tr("线程"));
    startBtn = new QPushButton(tr("开始"));
    stopBtn = new QPushButton(tr("停止"));
    quitBtn = new QPushButton(tr("退出"));

    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(startBtn);
    mainLayout->addWidget(stopBtn);
    mainLayout->addWidget(quitBtn);


    //信号槽连接
    connect(startBtn,SIGNAL(clicked()),this,SLOT(slotStart()));
    connect(stopBtn,SIGNAL(clicked()),this,SLOT(slotStop()));
    connect(quitBtn,SIGNAL(clicked()),this,SLOT(close()));

}

ThreadDlg::~ThreadDlg()
{
    delete ui;
}

void ThreadDlg::slotStart()
{
    for(int i = 0;i < MAXSIZE;++i)
    {
        workThread[i] = new WorkThread();
    }
    for(int i = 0;i < MAXSIZE;++i)
    {
        workThread[i]->start();
    }
    startBtn->setEnabled(false);
    stopBtn->setEnabled(true);
}

void ThreadDlg::slotStop()
{
    for(int i = 0;i < MAXSIZE;++i)
    {
        workThread[i]->terminate();
        workThread[i]->wait();
    }
    startBtn->setEnabled(true);
    stopBtn->setEnabled(false);
}

